/* 设置医院经纬度 调用百度地图API */
function SetLngLat(hospital, id, city, xy, fn) {
    var obj = this;
    this.init = function (hospital, id, city, fn) {
        obj.initModal();
        obj.initMap(hospital, id, city, fn);
        obj.getCopy();
        obj.modal.iziModal('open');
    };
    this.html = '<div class="box-body">' +
            '<div class="col-md-12">' +
                '<div class="col-md-6" style="padding: 0;">' +
                    '<input type="text" class="form-control search-hospital" value="' + hospital + '" placeholder="医院名称">' +
                '</div>' +
                '<div class="col-md-1" style="padding-left: 0;">' +
                    '<button type="button" class="btn bg-aqua search">查找</button>' +
                '</div>' +
                '<div class="col-md-4" style="padding: 0;">' +
                    '<input type="text" class="form-control lng-lat" placeholder="经度,纬度" value="' + (xy || '') + '">' +
                '</div>' +
                '<div class="col-md-1" style="padding: 0;">' +
                    '<button type="button" class="btn bg-aqua copy">复制</button>' +
                '</div>' +
            '</div>' +
            '<div class="col-md-12">' +
                '<div class="map" style="height: 600px;margin-top: 15px;"></div>' +
            '</div>' +
        '</div><div class="box-footer text-right">' +
            '<button data-izimodal-close="" class="btn btn-default cancel">取消</button>' +
            '<button type="button" class="btn bg-aqua save" style="margin-left: 10px;">保存</button>' +
        '</div>';
    this.initModal = function () {
        obj.modal = $('<div></div>');
        obj.modal.html(obj.html);
        $('body').append(obj.modal);
        obj.modal.iziModal({
            title: '设置医院经纬度',
            headerColor: '#00c0ef',
            theme: '',  // light
            attached: '', // bottom, top
            icon: null,
            iconText: null,
            iconColor: '',
            rtl: false,
            width: 1200,
            padding: 0,
            radius: 3,
            zindex: 1070,
            iframe: false,
            iframeHeight: 400,
            iframeURL: null,
            focusInput: false,
            group: '',
            loop: false,
            navigateCaption: true,
            navigateArrows: true, // closeToModal, closeScreenEdge
            history: false,
            restoreDefaultContent: false,
            autoOpen: false, // Boolean, Number
            bodyOverflow: false,
            fullscreen: true,
            openFullscreen: false,
            closeOnEscape: true,
            overlay: true,
            overlayClose: false,
            overlayColor: 'rgba(0, 0, 0, 0.4)',
            timeout: false,
            onOpening: function(){},
            onOpened: function(){},
            onClosing: function(){},
            onClosed: function(){
                obj.modal.iziModal('destroy');
                obj.modal.remove();
            }
        });
    };
    this.initMap = function (hospital, id, city, fn) {
        // 百度地图API功能
        var map = new BMap.Map(obj.modal.find('.map')[0]);    // 创建Map实例
        map.centerAndZoom(city || new BMap.Point(116.404, 39.915), 11);


        map.enableScrollWheelZoom();
        map.enableInertialDragging();
        map.enableContinuousZoom();

        var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
        var top_left_citylist = new BMap.CityListControl({anchor: BMAP_ANCHOR_TOP_RIGHT,offset: new BMap.Size(10, 20)});// 左上角，添加城市列表
        var top_left_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT});  //左上角，添加默认缩放平移控件
        // var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右上角，仅包含平移和缩放按钮
        map.addControl(top_left_control);
        map.addControl(top_left_citylist);
        map.addControl(top_left_navigation);
        // map.addControl(top_right_navigation);


        var local = new BMap.LocalSearch(map, {
            renderOptions:{map: map}
        });
        local.search(city + hospital);

        map.addEventListener("tilesloaded", function () {
            map.addControl(top_left_citylist);
        });

        map.addEventListener("click", getLngLat);

        /*map.addEventListener("tilesloaded",function (e) {
            console.log(e);
            obj.lngLat = obj.modal.find('.lng-lat');
            obj.lngLat.val(e.target.gf.lng + ',' + e.target.gf.lat);
        });*/

        function getLngLat(e) {
            obj.lngLat = obj.modal.find('.lng-lat');
            obj.lngLat.val(e.point.lng + ',' + e.point.lat);
        }

        obj.modal.find('.search').click(function () {
            var wd = obj.modal.find('.search-hospital').val();
            local.search(wd);
        });
        obj.modal.find('.save').click(function () {
            var wd = obj.modal.find('.lng-lat').val();
            if (!wd || !/^\d+(\.\d+){0,1},\d+(\.\d+){0,1}$/.test(wd)) {
                flavrShowByTime('请点击地图选取医院经纬度或手动输入正确的经纬度！', null, 'error', false);
                return;
            }
            wd = wd.split(',');
            var lng = wd[0];
            var lat = wd[1];
            var formData = new FormData();
            formData.set('hospitalId', id);
            formData.set('hospitalName', hospital);
            formData.set('mapX', lng);
            formData.set('mapY', lat);
            $.ajax({
                type : "POST",
                url : "addOrUpdateHospital",
                data : formData,
                dataType: "json",
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success : function(data) {
                    try{
                        new $.flavr({content:data.msg,closeOverlay:true,closeEsc:true,autoclose:true,timeout:1500,buttons:[{text:'确定',style:'success',action:function(){}}]});
                        obj.modal.iziModal('close');
                        fn();
                    }catch(error){
                       console.log(error);
                    }
                },
                failure:function(error){
                    console.log(error);
                }
            });
        });
    };

    this.getCopy = function () {
        obj.modal.find('.copy').click(function (){
            var Url2 = obj.modal.find('.lng-lat')[0];
            Url2.select(); // 选择对象
            document.execCommand("Copy");
        });
    };


    this.init(hospital, id, city, fn);
}

